package com.ddn.service.room.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.ddn.mapper.roomManagement.GoodsMapper;
import com.ddn.model.roomManagement.Goods;
import com.ddn.model.roomManagement.GoodsBo;
import com.ddn.service.room.GoodsService;
import com.ddn.util.ResultData;
import com.ddn.util.ThreadPoolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author XieYp
 * @desc
 * @date 2023/1/1
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public ResultData findAll(HashMap<String, Object> map) {
        String pageNo = (String) map.get("page");
        String pageSize = (String) map.get("limit");

        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));

        // 查询全部
        List<Goods> list =  goodsMapper.findAll(map);



        PageInfo<Goods> info = new PageInfo<>( list );
        // 计数
        return ResultData.ok(list, (int) info.getTotal());
    }

    @Override
    public ResultData addGoods(HashMap<String, Object> map) {
        boolean isOk = goodsMapper.addGoods(map);
        if (isOk) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData deleteGoods(HashMap<String, Object> map) {
        boolean isOk = goodsMapper.deleteGoods(map);
        if (isOk){
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData updateGoods(HashMap<String, Object> map) {
        boolean isOk = goodsMapper.updateGoods(map);
        if (isOk){
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData deleteBatch(List<Integer> ids) {
        boolean isOk = goodsMapper.deleteBatch(ids);
        if (isOk) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData select() {
        List<String> goodsType = goodsMapper.select();
        if (goodsType !=null) {
            return ResultData.ok(goodsType);
        }
        return ResultData.fail();
    }

    @Override
    public ResultData importGoods(MultipartFile file) throws IOException {
        //加入线程池，解决海量数据导入
        //1.获取上传的文件，校验
        if(!file.isEmpty()){
            EasyExcel.read(file.getInputStream(), Goods.class, new ReadListener<Goods>() {
                List<Goods> list=new ArrayList<>();
                @Override
                public void invoke(Goods goods, AnalysisContext analysisContext) {
                    list.add(goods);
                    //每个线程最多处理2000条
                    if(list.size()>=2000){
                        List l1=new ArrayList(list);
                        list.clear();
                        //加入到线程池
                        ThreadPoolUtil.getInstence().executor.execute(()->{
                            goodsMapper.importGoods(l1);
                        });
                    }
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    ThreadPoolUtil.getInstence().executor.execute(()->{
                        goodsMapper.importGoods(list);
                    });
                }
            }).doReadAll();
            //3.返回结果
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public List<GoodsBo> exportAllGoods() {
        List<GoodsBo> goodsBoList= goodsMapper.exportAllGoods();
        return goodsBoList;
    }
}
